//
//  DDTWelcomeViewController.swift
//  weibo
//
//  Created by mac on 16/9/30.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class DDTWelcomeViewController: UIViewController {
    
    var iconViewCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加UI界面元素
        addUI()
     
    }
    //添加 动画
    override func viewDidAppear(animated: Bool) {
           moveAnimation()
    }
    
    /**
    添加UI界面元素
    */
    private func addUI() {
        
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(textLabel)
        //添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //background Top
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0))
        //background leading
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1, constant: 0))
        //background bottom
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: 0))
        //background trailing
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1, constant: 0))
        
        //iconView 约束
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0))
        
        iconViewCons = NSLayoutConstraint(item: iconView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(iconViewCons!)
        
        // textLabel 约束
        self.view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: self.iconView, attribute: .Bottom, multiplier: 1, constant: 8))
        
        
    }
    
 /// 头像动画
    private func moveAnimation() {
        
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.iconViewCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //动画显示字体
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                     self.textLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        //TODO回到首页
                        
                })
                
        }
    }
    

 /// MARK - 懒加载 控件

    private lazy var backgroundImageView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    //图像
    private lazy var iconView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        return iconView
    }()
    
    //label
    private lazy var textLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.text = "欢迎归来"
        textLabel.textColor = UIColor.lightGrayColor()
        textLabel.sizeToFit()
        textLabel.alpha = 0
        return textLabel
    }()
    
    
    
}








